package com.vik.webgallery.beans;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.springframework.web.multipart.MultipartFile;

public class Image {
	
	private BufferedImage bi = null;
	
	
	public Image(MultipartFile f) throws IOException{
		this.bi = ImageIO.read(f.getInputStream());
	}

	
	public void resizeImage(int width, int height){
		
		int type = this.bi.getType() == 0? BufferedImage.TYPE_INT_ARGB : this.bi.getType();
		
		BufferedImage resizedImage = new BufferedImage(width, height, type);
		Graphics2D g = resizedImage.createGraphics();
		g.drawImage(this.bi, 0, 0, width, height, null);
		g.dispose();
	 
		this.bi = resizedImage;
	}
	
	public void saveImage(String filePath) throws IOException{
		ImageIO.write(this.bi, "jpg", new File(filePath));
	}
	
	public boolean isImage(){
		return bi == null ? false : true;
	}
}
